<?php

defined('SYSPATH') or die('Access error!');

class Model_Q2a_Comment extends Model_Q2a_Post
{

    /**
     *  插入问题
     * @param array $data
     * @return type
     */
    public function insert_comment($data)
    {
        return $this->insert_post($data);
    }

    public function get_comment_by_parent_id($id)
    {
        $where = " WHERE p.post_type='" . Q2A_POST_TYPE_COMMENT. "'";
        $where .=!empty($id) ? ' AND ' . 'p.parent_id=' . $id : '';
        $where .= ' ORDER BY p.position,p.id';
        $sql = "SELECT p.id,p.is_top,p.add_time,p.content,p.up_votes,p.down_votes,p.comment_count,u.username,u.avatar,u.id as user_id " .
                "FROM " . $this->table . " AS p " .
                "LEFT JOIN " . $this->db->get_table('user') . " AS u ON p.user_id = u.id " . $where;
        $comment_list_temp = $this->db->get_results($sql, ARRAY_A);
        $comment_list = array();
        if (count($comment_list_temp) > 0)
        {
            foreach ($comment_list_temp as $key => $value)
            {
                $value['avatar_path'] = Model::factory('user')->create_avatar_path($value['user_id'],$value['avatar']);
                $value['add_time'] = Date::timeformat($value['add_time'], 3);
                $comment_list[] = $value;
            }
            unset($comment_list_temp);
        }
        return $comment_list;
    }

}